package com.sdkj.dspservice.modules.sys.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdkj.dspservice.common.base.entity.BaseResult;
import com.sdkj.dspservice.common.constant.HttpStatus;
import com.sdkj.dspservice.common.react.AntdProResult;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.common.utils.UserUtils;
import com.sdkj.dspservice.modules.sys.entity.User;
import com.sdkj.dspservice.modules.sys.service.UserRoleService;
import com.sdkj.dspservice.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * 业务表Controller
 *
 * @author tm
 * @version 2020/12/28 18:54
 */
@RestController
@RequestMapping(value = "${project-config.apiUrl}/${project-config.version}/sys/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;

    @RequiresPermissions("sys:user:view")
    @RequestMapping(value = "get")
    public AntdProResult get(String id, HttpServletResponse response) {
        AntdProResult result = new AntdProResult();
        if (StringUtils.isNotBlank(id)) {
            User user = userService.getById(id);
            result.setData(user);
        } else {
            result.error(HttpStatus.STATUS_400, "id不能为空");
        }
        return result;
    }

    @RequiresPermissions("sys:user:view")
    @RequestMapping(value = "list")
    public AntdProResult list(User user) {
        AntdProResult result = new AntdProResult();
        IPage<User> userPage = userService.pageLink(new Page<>(user.current, user.pageSize), new QueryWrapper<>(user));
        result.success(userPage);
        return result;
    }

    @RequiresPermissions("sys:user:edit")
    @RequestMapping(value = "save", produces = MediaType.APPLICATION_JSON_VALUE)
    public AntdProResult save(@Valid @RequestBody User user) {
        AntdProResult result = new AntdProResult();
        // 创建时账号需要唯一
        if(user.isNewRecord()){
            Integer count = userService.getCountByLoginName(user.getLoginName());
            if(count > 0){
                result.error(AntdProResult.VALIDATOR_ERROR, "该账号已存在");
            }
        }
        if(result.getSuccess()){
            userService.saveOrUpdate(user);
        }
        return result;
    }

    @RequiresPermissions("sys:user:edit")
    @RequestMapping(value = "remove")
    public BaseResult delete(String id, HttpServletResponse response) {
        BaseResult result = new BaseResult();
        if (StringUtils.isNotBlank(id)) {
            userService.removeById(id);
        } else {
            response.setStatus(HttpStatus.STATUS_400);
            result.setErrorMessage("id不能为空");
        }
        return result;
    }

    /**
     * 获取当前用户
     * @return
     */
    @RequestMapping("getCurrentUser")
    public Map<String, Object> getCurrentUser(){
        return UserUtils.getCurrentUser().makeAntdUser();
    }

    /**
     * 获取当前用户
     * @return
     */
    @RequestMapping("currentUser")
    public AntdProResult currentUser(){
        AntdProResult result = new AntdProResult();
        result.success(UserUtils.getCurrentUser());
        return result;
    }

}